package com.tanhua.manage.controller;

import com.tanhua.manage.service.UserFreezeService;
import com.tanhua.manage.service.UserService;
import com.tanhua.manage.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/manage/users")
public class UsersController {

    @Autowired
    private UserService userService;


    /**
     * 用户数据翻页
     */
    @GetMapping
    public Pager<UserVo> queryByPage(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                     @RequestParam(name = "pagesize", defaultValue = "10") Integer pageSize,
                                     @RequestParam(name = "id", required = false) Long id,
                                     @RequestParam(name = "nickname", required = false) String nickname,
                                     @RequestParam(name = "city", required = false) String city) {
        return this.userService.queryByPage(page, pageSize, id, nickname, city);
    }


    /**
     * 用户基本资料
     */
    @GetMapping("/{userId}")
    public UserVo queryUserInfo(@PathVariable(name = "userId") Long userId) {
        return this.userService.queryUserInfo(userId);
    }

    @Autowired
    private UserFreezeService userFreezeService;
    /**
     * 冻结操作
     *
     * @param vo
     * @return
     */
    @PostMapping("freeze")
    public Boolean freeze(@RequestBody UserFreezeVo vo, HttpServletRequest httpServletRequest) {
        // 获取请求ip地址 127.0.0.1
        String ipAddresses = httpServletRequest.getHeader("X-Forwarded-For");
        System.out.println(ipAddresses);//127.0.0.1
        return this.userFreezeService.freeze(vo,ipAddresses);
    }



    /**
     * 解冻操作
     *
     * @param vo
     * @return
     */
    @PostMapping("/unfreeze")
    public Boolean unfreeze(@RequestBody UserFreezeVo vo, HttpServletRequest httpServletRequest) {
        // 获取请求ip地址 127.0.0.1
        String ipAddresses = httpServletRequest.getHeader("X-Forwarded-For");
        System.out.println(ipAddresses);//127.0.0.1
        return userFreezeService.unfreeze(vo,ipAddresses);
    }

    /**
     * 查询留存率
     * @param page
     * @param pageSize
     * @param sex
     * @param oddEven
     * @param sd
     * @param ed
     * @return
     */
    @GetMapping("/userActiveState")
    public Pager<UserActiveVo> queryUserActiveStateByDate(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                                          @RequestParam(name = "pagesize", defaultValue = "10") Integer pageSize,
                                                          @RequestParam(name = "sex", required = false) String sex,
                                                          @RequestParam(name = "oddEven", required = false) String oddEven,
                                                          @RequestParam(name = "sd") String sd,
                                                          @RequestParam("ed") String ed) {
        return this.userService.queryUserActiveStateByDate(page, pageSize, oddEven, sex, sd, ed);
    }

    /**
     * 查询每日登录注册信息
     * @param page
     * @param pageSize
     * @param sex
     * @param oddEven
     * @param sd
     * @param ed
     * @return
     */
    @GetMapping("/UserAnalysis")
    public Pager<UserAnalysisVo> queryUserAnalysisByDate(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                                         @RequestParam(name = "pagesize", defaultValue = "10") Integer pageSize,
                                                         @RequestParam(name = "sex", required = false) String sex,
                                                         @RequestParam(name = "oddEven", required = false) String oddEven,
                                                         @RequestParam(name = "sd") String sd,
                                                         @RequestParam("ed") String ed) {
        return this.userService.queryUserAnalysisByDate(page, pageSize, oddEven, sex, sd, ed);
    }
}